/**
 * Copyright © 2021 易久批信息技术有限公司. All rights reserved.
 */
package com.dysm.sort;

import java.util.Arrays;

/**
 * 动图演示：https://images2017.cnblogs.com/blog/849589/201710/849589-20171015225645277-1151100000.gif
 * 插入排序
 * @author xiay
 * @since 2021/1/30 10:58 上午
 */
public class InsertSortTest {

    public static void main(String[] args) {
        int[] arr = {9,5,8,3,2,4,6,7};
        System.out.println(Arrays.toString(arr));

        insertSort(arr);

        System.out.println(Arrays.toString(arr));
    }

    private static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int preIndex = i - 1;
            int currentValue = arr[i];
            while (preIndex > -1 && arr[preIndex] > currentValue){
                arr[preIndex + 1] = arr[preIndex];
                preIndex --;
            }
            arr[preIndex + 1] = currentValue;
        }
    }


}
